// 基地址
// const BASE_URL = 'http://192.168.0.184:84'
const BASE_URL = 'https://xhoven.com'

// 封装请求方法
export const request = options => {
  const header = {
    'content-type': options.header || 'application/json;charset=UTF-8',
    Authorization: `Bearer  ${uni.getStorageSync('token')}`
  }
  return new Promise((resolve, reject) => {
    uni.request({
      header,
      url: BASE_URL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      dataType: 'json',
      success: res => {
        if (res.statusCode === 403) {
          uni.navigateTo({ url: '/subpackages/login/login' })
          uni.showToast({
            title: '请先登录!',
            icon: 'error'
          })
          return
        }
        if (+res.data.code) {
          if (!options.hideToast) {
            uni.showToast({
              title: res.data.msg,
              icon: 'error'
            })
          }
          reject(res.data)
          return
        }
        resolve(res.data)
      },
      fail: err => {
        uni.showToast({
          title: '请求接口失败'
        })
        reject(err)
      }
    })
  })
}
